package com.ws.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.entity.Role;
import com.ws.mapper.RoleMapper;
import com.ws.service.RoleService;
import com.ws.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result getRoleListForPage(int pno, int psize) {
        Page<Role> p = new Page<>(pno,psize);
        Page<Role> pageResult = roleMapper.getRoleListForPage(p);
        //提取分页数据
        List<Role> roleList = pageResult.getRecords();

        JSONObject data = new JSONObject();
        data.put("list",roleList);

        //传入分页相关参数
        JSONObject page = new JSONObject();
        page.put("pno",pno);
        page.put("psize",psize);
        page.put("pCount",pageResult.getPages());
        page.put("totalElements",pageResult.getTotal());
        data.put("page",page);
        return Result.end(200,data,"查询成功");
    }

    @Override
    public Result roleInsert(Role role) {
        int count = roleMapper.insert(role);
        if(count > 0) {
            return Result.end(200,"","新增成功");
        } else {
            return Result.end(500,"","新增失败");
        }
    }

    @Override
    public Result deleteRoleById(Long roleId) {
        int count = roleMapper.deleteById(roleId);
        if(count > 0) {
            return Result.end(200,"","删除成功");
        } else {
            return Result.end(500,"","删除失败");
        }
    }

    @Override
    public Result findRoleById(Long roleId) {
        Role role = roleMapper.selectById(roleId);
        return Result.end(200,role,"查询成功");
    }

    @Override
    public Result updateRole(Role role) {
        int count = roleMapper.updateById(role);
        if(count > 0) {
            return Result.end(200,"","修改成功");
        } else {
            return Result.end(500,"","修改失败");
        }

    }


}
